/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converter.xml2brf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.brailleblaster.*;
import org.brailleblaster.louisutdml.*;
import org.liblouis.liblouisutdml;
import constants.Constants;
import java.io.PrintWriter;
import java.util.Scanner;
/**
/**
 *
 * @author CSE
 */
//currently support grade 0, grade1 and grade 2 english
public class XML2PEFConverter {
  public File xml2Pef(){
      //ProcessBuilder pb1 = new  ProcessBuilder("C:\\Program Files\\Java\\jdk1.7.0_01\\bin\\java.exe", "-jar", "C:\\LOUISE\\brailleblaster-1.3.0\\brailleblaster.jar","-nogui","translate","C:/Users/CSE/Desktop/pef_docs/matheqn.xml","C:/Users/CSE/Desktop/pef_docs/matheqn.brf");
     Runtime run = Runtime.getRuntime(); 
    String txtDir=null;
    String brfDir=null;
    String pefDir=null;
    String slash=null;
    String exe=null;
    String liblouisePath=null;
    String brailleUtilsPath=null;
    String mode=null;
    try{
    Properties prop = new Properties();
    
    InputStream is = new FileInputStream("application.properties");

    prop.load(is);

    txtDir=prop.getProperty(Constants.TEXT_FILE_LOC);
    brfDir=prop.getProperty(Constants.BRF_FILE_LOC);
    pefDir=prop.getProperty(Constants.PEF_FILE_LOC);
    exe=prop.getProperty(Constants.JAVA_EXE);
    liblouisePath=prop.getProperty(Constants.LIBLOUIS_PATH);
    brailleUtilsPath=prop.getProperty(Constants.BRAILLEUTILS_PATH);
    mode=prop.getProperty(Constants.MODE);
    
    }
    catch(Exception ex){
       
        ex.printStackTrace();
        return null;
    }
    
try 
{    slash=Constants.PATH_SEPERATOR;
     String fileName="new2.html";
     String txtFile=txtDir+slash+fileName;
     String brftFile=brfDir+slash+fileName+Constants.BRF;
     String pefFile=pefDir+slash+fileName+Constants.PEF;
    
     String table="en-us-comp6";
     this.selectTable(table);
     //before this should update the table.if no arguments send for this then it is an image.do consider en_us_comp-6 fior a table
    ProcessBuilder pb1 = new  ProcessBuilder(exe,Constants.JAR, liblouisePath,Constants.NOGUI,Constants.TRANSLATE,txtFile,brftFile);
   //ProcessBuilder pb2=new ProcessBuilder(exe, Constants.JAR, brailleUtilsPath,Constants.TEXT2PEF,brftFile,pefFile,mode);
    ProcessBuilder pb2=new ProcessBuilder(exe, Constants.JAR, brailleUtilsPath,Constants.TEXT2PEF,brftFile,pefFile,"-mode=NABCC");
     Process p = pb1.start(); 
    
     Process p2=pb2.start();
     File f=new File(pefFile);
     if(f.exists()){
         return f;
     }
     return null;
}
catch(Exception E) 
{
     E.printStackTrace();
     System.out.println(E.getMessage());   
     return null;
}
  }

    private void selectTable(String table) {
        try{
        File temp = new File("C:/LOUISE/brailleblaster-1.3.0/programData/liblouisutdml/lbu_files/preferences_template.cfg");
        File input = new File("C:/Users/CSE/Documents/NetBeansProjects/txtToBraille/src/template/preferences.cfg");
        Scanner sc = new Scanner(input);    //Reads from input
        PrintWriter pw = new PrintWriter(temp); //Writes to temp file

        while(sc.hasNextLine()){
           
            String s=sc.nextLine();
            if(s.split("%").length==2){
                System.out.println(s.split("%")[1]);
                s=s.split("%")[0]+"en-us-comp6.ctb";
                pw.println(s);
            }
            else
            pw.println(s);}
            
        sc.close();
        pw.close();
        
        File rep=new File("C:/LOUISE/brailleblaster-1.3.0/programData/liblouisutdml/lbu_files/preferences.cfg");
        
        rep.delete();
        temp.renameTo(rep);
        }
        catch(Exception ex){
            System.out.println("file not found");
        }
    }
}
